<template>
  <div>
    <el-form :inline="true">
      <el-form-item style="margin-left: 5px">
        <el-input
            v-model="searchForm.name"
            placeholder="请输入专利名称"
            clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="getPatentList">搜索</el-button>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="$router.push('/sys/application')">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-popconfirm title="这是确定批量删除吗？" @confirm="delHandle(null)">
          <el-button type="danger" slot="reference" :disabled="delBtlStatu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column
          prop="patent.name"
          label="专利名称"
          width="120">
      </el-table-column>
      <el-table-column
          prop="patent.type"
          label="专利类型"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="patent.content"
          label="专利简介"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="helper.name"
          label="代理人"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="patent.createTime"
          label="创建时间"
          show-overflow-tooltip>
      </el-table-column>

      <el-table-column
          prop="patent.status"
          label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.patent.status === 5" type="success">已通过</el-tag>
          <el-tag size="small" v-if="scope.row.patent.status === 6" type="danger">在售</el-tag>
        </template>

      </el-table-column>
      <el-table-column
          prop="icon"
          label="操作">

        <template slot-scope="scope">
          <el-button type="text" @click="lookCertificate(scope.row.patent.id,scope.row)">
            查看证书
          </el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="lookFee(scope.row.patent.id,scope.row)">
            查看费用
          </el-button>
          <el-divider direction="vertical"></el-divider>

          <el-button type="text" @click="sole(scope.row.patent.id,scope.row)" v-if="scope.row.patent.status === 5">
            出售
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status === 5"></el-divider>

          <el-button type="text" @click="revokeSole(scope.row.patent.id,scope.row)"
                     v-if="scope.row.patent.status === 6">
            撤销出售
          </el-button>
          <el-divider direction="vertical" v-if="scope.row.patent.status === 6"></el-divider>


          <el-button type="text" @click="look(scope.row.patent.id,scope.row)">
            查看
          </el-button>
          <el-divider direction="vertical"></el-divider>

          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.patent.id)"
                           v-if="scope.row.contact===true">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>

        </template>
      </el-table-column>

    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :current-page="current"
        :page-size="size"
        :total="total">
    </el-pagination>

    <el-dialog :title="form1.code" :visible.sync="dialogFormVisible1">
      <el-descriptions class="margin-top" :title="form1.title" :column="1">
        <el-descriptions-item label="专利名称">{{form1.name}}</el-descriptions-item>
        <el-descriptions-item label="发明人">{{form1.applicant}}</el-descriptions-item>
        <el-descriptions-item label="专利号">{{form1.patentCode}}</el-descriptions-item>
        <el-descriptions-item label="专利申请日">{{form1.create}}</el-descriptions-item>
        <el-descriptions-item label="专利权人">{{ form1.contact }}</el-descriptions-item>
        <el-descriptions-item label="地址">{{form1.address}}</el-descriptions-item>
        <el-descriptions-item label="描述">{{this.content}}</el-descriptions-item>
        <el-descriptions-item label="局长">申长雨</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <el-dialog title="专利费用" :visible.sync="dialogFormVisible2">
      <el-descriptions class="margin-top" title="专利费用说明" :column="1">
        <el-descriptions-item label="申请费">{{form2.applicationFee}}</el-descriptions-item>
        <el-descriptions-item label="维持费">{{form2.maintenanceFee}}</el-descriptions-item>
        <el-descriptions-item label="审查费">{{form2.examinationFee}}</el-descriptions-item>
        <el-descriptions-item label="专利登记、印刷、印花费">{{form2.otherFee}}</el-descriptions-item>
        <el-descriptions-item label="附加费">{{form2.extraFee}}</el-descriptions-item>
        <el-descriptions-item label="年费">{{form2.annualFee}}</el-descriptions-item>
        <el-descriptions-item label="总计">{{form2.total}}</el-descriptions-item>
        <el-descriptions-item label="缴纳状态">{{form2.status}}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible2 = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment';

export default {
  name: "PatentFee",
  data() {
    return {
      searchForm: {},
      delBtlStatu: true,

      total: 0,
      size: 10,
      current: 1,

      tableData: [],
      multipleSelection: [],
      status: '',
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      form1: {
        code: '',
        type: '',
        name: '',
        applicant: '',
        patentCode: '',
        create: '',
        contact: '',
        address: '',
        title:'',
      },
      form2: {
        applicationFee: '',
        maintenanceFee: '',
        examinationFee: '',
        otherFee: '',
        extraFee: '',
        annualFee: '',
        total: '',
        status: '',
      },
      first:'本发明经过本局依照中华人民共和国专利法进行审查，决定授予专利权，颁发',
      second:'专利证书并在专利登记簿上予以登记，专利权自授权公告之日生效，专利权极限为十年，自申请日起算。     '+
          '专利证书记载专利权登记时的法律状况，专利权的转移、质押、无效、终止、恢复和专利人的姓名或名称、国籍、地址变更等事项记载在专利登记簿上。',
      content:''
    }
  },
  created() {
    this.getPatentList()
  },
  methods: {
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;

      this.delBtlStatu = val.length == 0
    },

    handleSizeChange(val) {
      this.size = val
      this.getPatentList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getPatentList()
    },

    getPatentList() {
      //根据名称查询或查询所有专利和其代理人
      this.$axios.get("/patent/getP&H", {
        params: {
          id: this.$store.state.userId,
          name: this.searchForm.name,
          current: this.current,
          size: this.size,
          status1: 5,
          status2: 6
        }
      }).then(res => {
        this.tableData = res.data.data;
        this.tableData.forEach(row => {
          row.patent.createTime = moment(row.patent.createTime).format('YYYY-MM-DD HH:mm');
        });
        this.total = this.tableData[0].total;
      })
    },
    delHandle(id) {

      var ids = []

      if (id) {
        ids.push(id)
      } else {
        this.multipleSelection.forEach(row => {
          ids.push(row.patent.id)
        })
      }

      this.$axios.post("/patent/delete", ids).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose: () => {
            this.getPatentList()
          }
        });
        console.log(res.data);
      })
    },
    look(id, row) {
      this.$router.push({
        name: "sys:patent:application",
        params: {
          patentId: id,
          disable: true,
        }
      })
    },
    revokeSole(id, row) {
      this.status = 'revokeSole';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 5;
      })
    },
    sole(id, row) {
      this.status = 'sole';
      this.$axios.post("/patent/statusChange/" + this.status + "/" + id).then(res => {
        if (res.data.data === 1) {
          this.$message({
            message: '恭喜你，操作成功！',
            type: 'success'
          });
        }
        row.patent.status = 6;
      })
    },
    lookFee(id, row) {
      this.dialogFormVisible2 = true;
      this.$axios.get("/patent/fee/" + id).then(res => {
        console.log(res.data);
        this.form2.applicationFee=res.data.data.applicationfee;
        this.form2.maintenanceFee=res.data.data.maintenancefee;
        this.form2.examinationFee=res.data.data.examinationfee;
        this.form2.otherFee=res.data.data.otherfee;
        this.form2.extraFee=res.data.data.extrafee;
        this.form2.annualFee=res.data.data.annualfee;
        if(res.data.data.status===1){
          this.form2.status='已缴纳';
        }else {
          this.form2.status='未缴纳';
        }
        this.form2.total = this.form2.maintenanceFee+ this.form2.examinationFee+ this.form2.otherFee+ this.form2.extraFee+this.form2.annualFee+this.form2.applicationFee;
      });
    },
    lookCertificate(id, row) {
      this.dialogFormVisible1 = true;
      this.$axios.get("/patent/certificate/" + id,).then(res => {
        this.form1.type = row.patent.type;
        this.content=this.first+this.form1.type+this.second;
        this.form1.title = this.form1.type+"证书";
        this.form1.name = row.patent.name;
        this.form1.code = "证书号："+res.data.data.code+"号";
        this.form1.patentCode = res.data.data.patentcode;
        this.form1.create = row.patent.createTime;
      });
      this.$axios.get('/patent/selectP&A/' + row.patent.id)
          .then(res => {
            res.data.data.patent.applicants.forEach(applicant=>{
              if(applicant.hasContact===true){
                this.form1.applicant = applicant.name;
                this.form1.contact = applicant.name;
                this.form1.address = applicant.address;
              }
            })
          })
    }
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>